package myapp.camera;


import java.util.ArrayList;

import myapp.DTO.ServiceDTO;
import myapp.DTO.SubItemDTO;
import myapp.dictionary.MyDictionary;
import myapp.googlemap.R;
import myapp.helper.OrientationHelper;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<ServiceDTO> {

	private Context context;
	private int layoutResourceId;   
	private ArrayList<ServiceDTO> data = null;
	private ArrayList<SubItemDTO> subItems = null;
	private boolean discount_information = false;
	public ItemAdapter(Context context, int textViewResourceId,  ArrayList<ServiceDTO> objects, ArrayList<SubItemDTO> _subItems, boolean _discount_information) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = textViewResourceId;
		this.data = new ArrayList<ServiceDTO>();
		for (ServiceDTO service : objects)
			this.data.add(service);
		this.subItems = _subItems;
		this.discount_information = _discount_information;
	}
	
	public void addAllSubItems(ArrayList<SubItemDTO> _subs)
	{
		if (subItems == null)
			subItems = new ArrayList<SubItemDTO>();
		subItems.addAll(_subs);
	}
	
	public void clearSubItems() {
		subItems.clear();
	}
	
	public void setDiscountInformation(boolean value) {
		this.discount_information = value;
	}
	
	public void updateArrayList( ArrayList<ServiceDTO> objects)
	{
		this.data.clear();
		if (objects != null)
			for (ServiceDTO service : objects)
				this.data.add(service);
	}
	

	 @Override
	 public View getView(int position, View convertView, ViewGroup parent) {
		 	View row = convertView;
	        ItemHolder holder = null;
	       
	        if(row == null)
	        {
	            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            row = inflater.inflate(layoutResourceId, parent, false);
	           
	            holder = new ItemHolder();
	            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
	            holder.txtname = (TextView)row.findViewById(R.id.text_view_Name);
	            holder.txtAddress = (TextView)row.findViewById(R.id.text_view_Address);
	            holder.txtDistance = (TextView)row.findViewById(R.id.text_view_Distance);
	            
	            row.setTag(holder);
	        }
	        else
	        {
	            holder = (ItemHolder)row.getTag();
	        }
	        try
	        {	       
	        Log.i("ItemAdapter_getView", String.valueOf(position));
	        ServiceDTO service = data.get(position);
	        holder.txtname.setText(service.getName());
	        
	        holder.txtAddress.setText(service.getAddress());
	        if (discount_information == true)
	        	for (int i = 0 ; i < subItems.size(); i++)
	        		if (subItems.get(i).serviceId == service.getId())
	        		{
	        			holder.txtAddress.setText(subItems.get(i).details);
	        			break;
	        		}	        	
	        
	        double distance = OrientationHelper.CalculateDistanceBetweenTwoPoints(OrientationHelper.getCurLon(), OrientationHelper.getCurLat(), service.getLongitude(), service.getLatitude());
	        holder.txtDistance.setText(String.valueOf((int)distance) + "m");
	        holder.imgIcon.setImageResource(MyDictionary.dictionary.get(service.getCategoryId(), 0));
			}catch(Exception ex)
			{
				Log.i("ItemAdapter_getView", ex.toString());
			}	        
	        return row;
	    }
	   
	    static class ItemHolder
	    {
	        ImageView imgIcon;
	        TextView txtname;
	        TextView txtAddress;
	        TextView txtDistance;
	    }

}
